<template>
	<view class="goods-detail-container">
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
		  <swiper-item v-for="(item,i) in goodsInfo.pics" :key="i" @click="preview(i)">
		    <image :src="item.pics_big"></image>
		  </swiper-item>
		</swiper>
    <view class="goodsItem-box" v-if="goodsInfo.goods_name">
      <view class="goods-price">
        ￥{{goodsInfo.goods_price}}
      </view>
      <view class="goods-body">
        <view class="goods-info">
          {{goodsInfo.goods_name}}
        </view>
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yf">
        快递：免运费
      </view>
    </view>
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" class="goods-nav"/>
	</view>
</template>

<script>
  import {mapState} from 'vuex'
  import {mapMutations,mapGetters} from 'vuex'
  
	export default {
    computed:{
      ...mapState('m_cart',['cart']),
      ...mapGetters('m_cart',['total'])
    },
    watch:{
      total:{
        handler(newVal){
          this.options.find((x)=>x.text=== '购物车').info=newVal
        },
        immediate:true
      }
    },
		data() {
			return {
				goodsInfo:{},
        options: [{
          icon: 'shop',
          text: '店铺'
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        // 右侧按钮组的配置对象
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
			};
		},
    onLoad(options) {
      this.getGoodsInfo(options.goods_id)
      // console.log(options.goods_id)
    },
    methods:{
      async getGoodsInfo(goods_id){
        const {data :res} = await uni.$http.get('/api/public/v1/goods/detail', { goods_id })
        // console.log(res)
        if(res.meta.status!==200) return uni.$showMsg()
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,'<img style="display:block;" ').replace(/webp/g,'jpg')
        this.goodsInfo = res.message
      },
      preview(i){
        uni.previewImage({
          current:i,
          urls:this.goodsInfo.pics.map(x=>x.pics_big)
        })
      },
      onClick(e){
        if(e.content.text==='购物车'){
          uni.switchTab({
            url:'../../pages/cart/cart'
          })
        }
      },
      ...mapMutations('m_cart',['addToCart']),
      buttonClick(e){
        if(e.content.text === '加入购物车'){
          const goods = {
            goods_id: this.goodsInfo.goods_id,       // 商品的Id
             goods_name: this.goodsInfo.goods_name,   // 商品的名称
             goods_price: this.goodsInfo.goods_price, // 商品的价格
             goods_count: 1,                           // 商品的数量
             goods_small_logo: this.goodsInfo.goods_small_logo, // 商品的图片
             goods_state: true                         // 商品的勾选状态
          }
          this.addToCart(goods)
        }
      }
    }
	}
</script>

<style lang="scss">
swiper{
  height: 750rpx;
  
    image {
      width: 100%;
      height: 100%;
    }
}
.goods-detail-container{
  padding-bottom: 50px;
}
.goodsItem-box{
  padding: 10px;
  padding-right: 0px;
  .goods-price{
    color: #c00000;
    font-size: 18px;
    margin: 10px 0;
  }
  .goods-body{
    display: flex;
    justify-content: space-between;
    .goods-info{
      font-size: 13px;
      padding-right: 10px;
    }
    .favi{
      width: 120px;
      font-size: 12px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      border-left: 1px solid #efefef;
      color: gray;
    }
  },
  .yf{
    margin: 10px 0;
    font-size: 12px;
    color: gray;
  }
}
.goods-nav{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
</style>
